﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyHealthOnline.Business;
using MyHealthOnline.Dal.Generated.EntityClasses;

namespace MyHealthOnline.Specialist.Controls
{
    public partial class Questions : System.Web.UI.UserControl
    {
        public int QuestionnaireId
        {
            get
            {
                if (ViewState["QuestionnaireId"] == null)
                {
                    return -1;
                }
                return Convert.ToInt32(ViewState["QuestionnaireId"].ToString());
            }
            set
            {
                ViewState["QuestionnaireId"] = value;
            }
        }
        public QuestionnaireEntity Questionnaire
        {
            get
            {
                return QuestionnaireBusiness.GetQuestionnaire(QuestionnaireId);
            }
        }

        public int ConsultId
        {
            get
            {
                if (ViewState["ConsultId"] == null)
                {
                    return -1;
                }
                return Convert.ToInt32(ViewState["ConsultId"].ToString());
            }
            set
            {
                ViewState["ConsultId"] = value;
            }
        }
        public ConsultEntity Consult
        {
            get
            {
                return ConsultBusiness.GetConsult(ConsultId);
            }
        }

        public ListItemCollection QuestionTypeList
        {
            get
            {
                ListItemCollection coll = new ListItemCollection();
                coll.AddRange(new ListItem[]
                    {
                         new ListItem("Open Question"),
                         new ListItem("Multiple Choice Question"),
                         new ListItem("Question with Date"),
                         new ListItem("Question from Database")
                    });
                return coll;
            }
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ddlQuestionType.DataSource = QuestionTypeList;
                ddlQuestionType.DataBind();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (!IsQuestionnaire)
            {
                if (ddlQuestionType.SelectedValue == "Open Question")// "Open Question"
                {
                    QuestionBusiness.AddOpenQuestion(ConsultId, txtQuestion.Text, chkIncludeOpen.Checked);
                    txtQuestion.Text = "";
                }
                else if (ddlQuestionType.SelectedValue == "Multiple Choice Question")//"Multiple Choice Question"
                {
                    QuestionBusiness.AddMultiQuestion(ConsultId, txtMultipleChoiceQuestion.Text, txtAnswerChoices.Text, chkIncludeMulti.Checked);
                    txtMultipleChoiceQuestion.Text = "";
                    txtAnswerChoices.Text = "";
                }
                else if (ddlQuestionType.SelectedValue == "Question with Date")//"Question with Date"
                {
                    QuestionBusiness.AddDateQuestion(ConsultId, txtQuestionDate.Text, chkIncludeDate.Checked);
                    txtQuestionDate.Text = "";
                }
                else if (ddlQuestionType.SelectedValue == "Question from Database")//"Question from Database"
                {
                    int questionId = Convert.ToInt32(rblDatabaseQuestions.SelectedValue);
                    QuestionBusiness.AddDatabaseQuestion(ConsultId, questionId);
                    rblDatabaseQuestions.ClearSelection();
                }

                WebServiceHO webService = new WebServiceHO();
                webService.Notify(Consult.Specialist.Email, Consult.Pacient.Email,
                                "New Question asked. Please verify your account",
                                "New question asked");
            }
            else
            {
                if (ddlQuestionType.SelectedValue == "Open Question")// "Open Question"
                {
                    QuestionnaireBusiness.AddOpenQuestion(QuestionnaireId, txtQuestion.Text, chkIncludeOpen.Checked);
                    txtQuestion.Text = "";
                }
                else if (ddlQuestionType.SelectedValue == "Multiple Choice Question")//"Multiple Choice Question"
                {
                    QuestionnaireBusiness.AddMultiQuestion(QuestionnaireId, txtMultipleChoiceQuestion.Text, txtAnswerChoices.Text, chkIncludeMulti.Checked);
                    txtMultipleChoiceQuestion.Text = "";
                    txtAnswerChoices.Text = "";
                }
                else if (ddlQuestionType.SelectedValue == "Question with Date")//"Question with Date"
                {
                    QuestionnaireBusiness.AddDateQuestion(QuestionnaireId, txtQuestionDate.Text, chkIncludeDate.Checked);
                    txtQuestionDate.Text = "";
                }
                else if (ddlQuestionType.SelectedValue == "Question from Database")//"Question from Database"
                {
                    int questionId = Convert.ToInt32(rblDatabaseQuestions.SelectedValue);
                    QuestionnaireBusiness.AddDatabaseQuestion(QuestionnaireId, questionId);
                    rblDatabaseQuestions.ClearSelection();
                }
            }
        }

        protected void ddlQuestionType_SelectedIndexChanged(object sender, EventArgs e)
        {
            var ddl = sender as DropDownList;
            //pnlEndConsult.Visible = false;
            //pnlNextDate.Visible = false;
            //pnlQuestionnaire.Visible = false;
            //pnlMessage.Visible = false;
            if (ddl.SelectedValue == "Open Question")//"Open Question"
            {
                pnlQuestions.Visible = true;
                pnlOpenQuestion.Visible = true;
                pnlMultipleChoice.Visible = false;
                pnlQuestionWithDate.Visible = false;
                pnlDatabase.Visible = false;
                txtQuestion.Text = "";

            }
            else if (ddl.SelectedValue == "Multiple Choice Question")//"Multiple Choice Question"
            {
                pnlOpenQuestion.Visible = false;
                pnlQuestions.Visible = true;
                pnlMultipleChoice.Visible = true;
                pnlQuestionWithDate.Visible = false;
                pnlDatabase.Visible = false;

            }
            else if (ddl.SelectedValue == "Question with Date")//"Question with Date"
            {
                pnlOpenQuestion.Visible = false;
                pnlQuestions.Visible = true;
                pnlMultipleChoice.Visible = false;
                pnlQuestionWithDate.Visible = true;
                pnlDatabase.Visible = false;

            }
            else if (ddl.SelectedValue == "Question from Database")//"Question from Database"
            {
                pnlOpenQuestion.Visible = false;
                pnlQuestions.Visible = true;
                pnlMultipleChoice.Visible = false;
                pnlQuestionWithDate.Visible = false;
                pnlDatabase.Visible = true;

                rblDatabaseQuestions.DataTextField = "QuestionText";
                rblDatabaseQuestions.DataValueField = "Id";

                rblDatabaseQuestions.DataSource = QuestionBusiness.GetQuestionsFromDatabase();
                rblDatabaseQuestions.DataBind();

                if (rblDatabaseQuestions.Items.Count == 0)
                {
                    rblDatabaseQuestions.Visible = false;
                    lblNoDatabaseQuestions.Visible = true;
                }
                else
                {
                    rblDatabaseQuestions.Visible = true;
                    lblNoDatabaseQuestions.Visible = false;
                }
            }
        }

        public bool IsQuestionnaire { get; set; }
    }
}